using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ALib;
using ALib.Data.CSVOperation;

namespace TradingPlatform
{
    public class TickerInfoDb
    {
        private readonly SortedList<string, string[]> _tickerInfoList;

        private readonly ExchangeRate _exchangeRate;

        public ExchangeRate ExchangeRate
        {
            get { return _exchangeRate; }
        } 

        public List<string> TickerNameList
        {
            get
            {
                List<string> result = new List<string>();
                foreach (string tickerName in _tickerInfoList.Keys)
                {
                    result.Add(tickerName);
                }
                return result;
            }
        }

        public List<string> SymbolList
        {
            get
            {
                List<string> result = new List<string>();
                foreach (string[] tickerInfo in _tickerInfoList.Values)
                {
                    string symbol = tickerInfo[(int) TickerInfoItemType.symbol];
                    if (!result.Contains(symbol))
                    {
                        result.Add(symbol);
                    }
                }
                return result;
            }
        }
        
        public TickerInfoDb(FileInfo tickerInfoFile)
        {
            List<string[]> records = File.Exists(tickerInfoFile.FullName) ? CSVEditor.Open(tickerInfoFile.FullName, "//") : new List<string[]>();
            _tickerInfoList = new SortedList<string, string[]>();

            foreach (string[] record in records)
            {
                string exchange = record[(int)TickerInfoItemType.exchange];
                string secType = record[(int)TickerInfoItemType.secType];
                string symbol = record[(int)TickerInfoItemType.symbol];
                string expiry = record[(int)TickerInfoItemType.expiry];
                string currency = record[(int)TickerInfoItemType.currency];

                string tickerName = TickerNameAndId.ToTickerName(exchange, secType, symbol, expiry, currency);
                _tickerInfoList.Add(tickerName, record);
            }

            _exchangeRate = new ExchangeRate();
        }

        public TickerInfoDb()
            : this(new FileInfo(@"Configuration\TickerInfo.csv"))
        {
        }

        public string GetTickerInfoItem(string tickerName, TickerInfoItemType tickerInfoItemType)
        {
            GoodValue.Assert(_tickerInfoList.ContainsKey(tickerName));

            string result = _tickerInfoList[tickerName][(int) tickerInfoItemType];

            return result;
        }

        public double GetExchangeRateXxxUsd(string tickerName)
        {
            string currency = _tickerInfoList[tickerName][(int) TickerInfoItemType.strategyCurrency];
            double result = _exchangeRate.GetExchangeRateXxxUsd(currency);
            return result;
        }

        public string GetTickerName(string strategyTickerName)
        {
            string[] strategyTickerNameItems = strategyTickerName.Split(new char[] {'_'});
            List<string> found = new List<string>();
            foreach (KeyValuePair<string, string[]> tickerName_TickerInfoItems in _tickerInfoList)
            {
                string strategySymbol = tickerName_TickerInfoItems.Value[(int) TickerInfoItemType.strategySymbol];
                string expiry = tickerName_TickerInfoItems.Value[(int) TickerInfoItemType.expiry];

                if (strategyTickerNameItems[1] == strategySymbol && strategyTickerNameItems[2] == expiry)
                {
                    found.Add(tickerName_TickerInfoItems.Key);
                }
            }
            GoodValue.Assert(found.Count == 1);
            string result = found[0];
            return result;
        }
    }
}
